
package com.enuo.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * @ClassName: Role
 * @Description: TODO(Entity - 角色)
 * @author Charles
 * @date 2017年11月21日 下午1:49:02
 * @version 1.0
 */
@Entity
@Table(name = "role")
public class Role extends BaseEntity
{

    private static final long serialVersionUID = -6614052029623997372L;

    /** 名称 */
    private String name;

    /** 是否内置 */
    private Boolean isSystem;

    /** 描述 */
    private String description;

    /** 权限 */
    private List<String> authorities = new ArrayList<String>();

    /** 菜单 */
    private List<MenuItem> menuItems = new ArrayList<MenuItem>();

    /** 管理员 */
    private Set<Admin> admins = new HashSet<Admin>();

    @NotEmpty
    @Column(nullable = false)
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Column(nullable = false, updatable = false)
    public Boolean getIsSystem()
    {
        return isSystem;
    }

    public void setIsSystem(Boolean isSystem)
    {
        this.isSystem = isSystem;
    }

    /**
     * 获取描述
     * 
     * @return 描述
     */
    @Length(max = 200)
    public String getDescription()
    {
        return description;
    }

    /**
     * 设置描述
     * 
     * @param description
     *            描述
     */
    public void setDescription(String description)
    {
        this.description = description;
    }

    /**
     * 获取权限
     * 
     * @return 权限
     */
    @ElementCollection
    @CollectionTable(name = "xx_role_authority")
    public List<String> getAuthorities()
    {
        return authorities;
    }

    /**
     * 设置权限
     * 
     * @param authorities
     *            权限
     */
    public void setAuthorities(List<String> authorities)
    {
        this.authorities = authorities;
    }

    /**
     * 获取管理员
     * 
     * @return 管理员
     */
    @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
    public Set<Admin> getAdmins()
    {
        return admins;
    }

    /**
     * 设置管理员
     * 
     * @param admins
     *            管理员
     */
    public void setAdmins(Set<Admin> admins)
    {
        this.admins = admins;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "xx_role_menu_item")
    @OrderBy("order asc, id asc")
    public List<MenuItem> getMenuItems()
    {
        return menuItems;
    }

    public void setMenuItems(List<MenuItem> menuItems)
    {
        this.menuItems = menuItems;
    }
}